function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { getRegisterComponentWithProps, LayConfWrap, iup } from "../util";
import { Comp_Info } from "./config/baseConfig";
export { LayConfWrap };
var emptyArray = [];

/**
 * 根据配置数据获取对应配置的组件
 * @param {object} item 组件配置项
 * @returns {object} 组件
 */
export function getComp(item) {
  if (!item.xtype) return {
    instance: Comp_Info['Input'].instance
  };
  if (typeof item.xtype === 'function') return {
    instance: item.xtype
  };
  if (item.xtype && _typeof(item.xtype) === 'object' && item.xtype.render && item.xtype.$$typeof) return {
    instance: item.xtype
  };
  return function (xtype, type) {
    if (Comp_Info[xtype]) {
      if (xtype === 'Input' && ['prc', 'amt', 'amount', 'qty', 'rate', 'percent'].includes(type) && Comp_Info['InputNumber']) {
        return {
          instance: Comp_Info['InputNumber'].instance,
          props: Comp_Info['InputNumber'].defaultProps
        };
      }
      return {
        instance: Comp_Info[xtype].instance,
        props: Comp_Info[xtype].defaultProps
      };
    }
    var _getRegisterComponent = getRegisterComponentWithProps(xtype),
      _getRegisterComponent2 = _slicedToArray(_getRegisterComponent, 2),
      ins = _getRegisterComponent2[0],
      props = _getRegisterComponent2[1];
    if (ins) {
      return {
        instance: ins,
        props: props
      };
    }
    return {
      instance: Comp_Info['Input'].instance
    };
  }(item.xtype, item.type);
}

/**
 * 获取组件对应的配置
 * @returns {object} 组件的配置props
 * @param item
 * @param config
 */
export function getCompPorps(item, config) {
  var p = config;
  var t = Object.keys(p).reduce(function (acc, curr) {
    acc[curr] = item[curr] ? item[curr] : p[curr]['defaultValue'];
    return acc;
  }, item.antProps || {});
  p['data'] && (t['data'] = (item === null || item === void 0 ? void 0 : item.data) || emptyArray);
  (item.xtype === 'Checkbox' || item.xtype === 'Checkbox') && delete t.value;
  return t;
}

/**
 * 格式化数据
 * @param name
 * @param type
 * @param opt
 * @param key
 * @param initVals
 */
export function fomatValues(name, type, opt, key, initVals) {
  var vals = initVals;
  return merge;
  function merge(values) {
    if (_typeof(vals) !== _typeof(values)) {
      vals = values || vals;
    } else if (Array.isArray(vals)) {
      vals = vals.concat(values);
    } else if (_typeof(vals) === 'object') {
      vals = _objectSpread(_objectSpread({}, vals), values);
    } else {
      vals = values;
    }
    if (!values) return _defineProperty({}, name, _defineProperty({}, type, vals ? _defineProperty({
      key: key
    }, opt, vals) : {
      key: key
    }));
    return merge;
  }
}
export function formFomatValues(key, initVals) {
  var vals = {};
  var optType = 'newRow';
  if (Object.prototype.toString.call(initVals) === '[object Object]') {
    initVals = [initVals];
  }
  if (Array.isArray(initVals)) {
    vals = initVals.reduce(function (p, n) {
      return _objectSpread(_objectSpread({}, p), n);
    }, {});
    return _defineProperty({}, 'form', _defineProperty({
      key: key
    }, vals[key] ? 'modifiedRow' : 'newRow', vals));
  }
  return merge;
  function merge(values) {
    if (_typeof(vals) !== _typeof(values)) {
      vals = values || vals;
    } else if (Array.isArray(vals)) {
      vals = vals.concat(values);
    } else if (_typeof(vals) === 'object') {
      vals = _objectSpread(_objectSpread({}, vals), values);
    } else {
      vals = values;
    }
    if (!values) {
      return _defineProperty({}, 'form', vals ? _defineProperty({
        key: key
      }, optType, vals) : {
        key: key
      });
    }
    if (values[key]) optType = 'modifiedRow';
    return merge;
  }
}
export function listToObj(c) {
  if (!c) return {};
  return c.reduce(function (acc, item) {
    acc[item.name] = item;
    return acc;
  }, {});
}
export function getProperty(o, keys) {
  if (typeof keys === 'string' || typeof keys === 'number') {
    return o && o[keys];
  }
  for (var i = 0; i < keys.length; i++) {
    if (!o) return;
    o = o[keys[i]];
  }
  return o;
}
export function parseFunction(str) {
  if (str && typeof str === 'string') {
    return function () {
      try {
        var fn = new Function('return ' + str)();
        return fn.apply(void 0, arguments);
      } catch (error) {
        console.log('erro:======', error, str);
      }
    };
  }
  return str;
}
export function getObjValue(obj, pathKeys) {
  return iup.getObjValue(obj, pathKeys);
}
export function setObjValue(obj, pathKeys, value) {
  iup.setObjValue(obj, pathKeys, value);
}
export function getSubscribeFn(outRef) {
  return function (fn, type, name) {
    if (!outRef.current._compIns) {
      return function () {};
    }
    if (name && name.length > 0) {
      var _outRef$current$inner, _outRef$current;
      var arr = iup.isArray(name) ? name : [name];
      var loop = function loop(obj) {
        var prevKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
        return Object.keys(obj).some(function (k) {
          if (iup.isObject(obj[k]) && loop(obj[k], k)) {
            return true;
          }
          return arr.includes(prevKey ? "".concat(prevKey, ".").concat(k) : k);
        });
      };
      return (_outRef$current$inner = (_outRef$current = outRef.current).innerSubscribe) === null || _outRef$current$inner === void 0 ? void 0 : _outRef$current$inner.call(_outRef$current, function (p) {
        fn(_objectSpread(_objectSpread({}, p), {}, {
          name: name
        }));
      }, type, function (_ref6) {
        var args = _ref6.args;
        return loop(args[0]);
      });
    } else {
      var _outRef$current$inner2, _outRef$current2;
      return (_outRef$current$inner2 = (_outRef$current2 = outRef.current).innerSubscribe) === null || _outRef$current$inner2 === void 0 ? void 0 : _outRef$current$inner2.call(_outRef$current2, fn, type);
    }
  };
}
export function isHelpField(type) {
  var _getRegisterComponent3;
  return iup.isString(type) && ((_getRegisterComponent3 = getRegisterComponentWithProps(type)[2]) === null || _getRegisterComponent3 === void 0 ? void 0 : _getRegisterComponent3.isHelp);
}
export function isValueLabel(item) {
  return iup.isObject(item) && item.hasOwnProperty('value') && item.hasOwnProperty('label');
}
export function convertHelpValues(initValues) {
  Object.keys(initValues).forEach(function (k) {
    var originValue = initValues[k];
    if (originValue !== null && originValue !== void 0 && originValue.length && isValueLabel(originValue[0])) {
      setObjValue(initValues, k, originValue.map(function (item) {
        return item.value;
      }).join());
    } else if (isValueLabel(originValue)) {
      setObjValue(initValues, k, originValue.value);
    }
  });
  return initValues;
}